// 浮点数精度 height - medium - lowp ( 放在最前面，告知计算机应该如何计算浮点数 )
precision lowp float;


uniform float uWaresFrequency;
uniform float uScale;
uniform float uXzScale;
uniform float uNoiseFrequency;
uniform float uNoiseScale;
uniform vec3 uLowColor;
uniform vec3 uHightColor;

uniform float uTime;

varying float vElevation;

float rand(vec2 co) {
    return fract(sin(dot(co.xy, vec2(12.9898, 78.233))) * 43758.5453);
}

float noise(vec2 st) {
    vec2 i = floor(st);
    vec2 f = fract(st);

    // Four corners in 2D of a tile
    float a = rand(i);
    float b = rand(i + vec2(1.0, 0.0));
    float c = rand(i + vec2(0.0, 1.0));
    float d = rand(i + vec2(1.0, 1.0));

    // Smooth Interpolation
    vec2 u = f * f * (3.0 - 2.0 * f);
    return mix(a, b, u.x) + (c - a) * u.y * (1.0 - u.x) + (d - b) * u.x * u.y;
}

void main(){
    vec4 modulePosition = modelMatrix * vec4(position,1.0);

    float elevation = sin(modulePosition.x * uWaresFrequency * uXzScale) * sin( modulePosition.z * uWaresFrequency * uXzScale);

    elevation += -abs(noise(vec2(modulePosition.xz*uNoiseFrequency + uTime)) * uNoiseScale);
    vElevation = elevation;

    elevation *= uScale;


    modulePosition.y += elevation;

    gl_Position = projectionMatrix * viewMatrix * modulePosition;
}